package com.nsgf.library.ui.page.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.kunminx.binding_recyclerview.adapter.BaseDataBindingAdapter;
import com.nsgf.library.BR;
import com.nsgf.library.R;
import com.nsgf.library.data.bean.MessageBean;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.data.bean.WorkOrderDetailBean;
import com.nsgf.library.data.repository.DataRepository;
import com.nsgf.library.data.response.DataResult;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.data.response.ResultSource;
import com.nsgf.library.databinding.ActivitySearchBinding;
import com.nsgf.library.databinding.FragmentNagfListBinding;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.adapter.PlaylistAdapter;
import com.nsgf.library.ui.page.BaseFragment;
import com.nsgf.library.ui.page.MessageDetailsActivity;
import com.nsgf.library.ui.page.StateHolder;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;

public class MessageFragment extends BaseFragment {

    public static MessageFragment newInstance(String type) {
        MessageFragment messageFragment = new MessageFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        messageFragment.setArguments(bundle);
        return messageFragment;

    }

    private String getType() {
        return getArguments().getString("type");
    }

    private MessageFragmentStates mStates;

    private PlaylistAdapter mAdapter;


    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(MessageFragmentStates.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);

    }

    private AccountRequester mAccountRequester;

    @Override
    protected DataBindingConfig getDataBindingConfig() {

        mAdapter = new PlaylistAdapter(getContext());

        return new DataBindingConfig(R.layout.fragment_nagf_list, BR.vm, mStates)
                .addBindingParam(BR.adapter, mAdapter);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getLifecycle().addObserver(mAccountRequester);
        mBinding = (FragmentNagfListBinding) getBinding();
        getDatas();


        mAdapter.setOnItemClickListener((viewId, item, position) -> {

            mAdapter.getCurrentList().get(position).setIsRead(1);
            mAdapter.notifyItemChanged(position);
            Intent intent = new Intent(getActivity(), MessageDetailsActivity.class);
            intent.putExtra("title", item.getTypeName());
            intent.putExtra("id", item.getId());
            startActivity(intent);
        });
    }

    private String search = "";


    private FragmentNagfListBinding mBinding;

    public void notifyDataSetChanged() {
        for (int i = 0; i < mAdapter.getCurrentList().size(); i++) {
            mAdapter.getCurrentList().get(i).setIsRead(1);
        }
        mAdapter.notifyDataSetChanged();
    }

    /**
     *
     * @param search
     */
    public void getDatas(String search) {
        this.search = search;
        DataRepository.getInstance().getMessage(getType(), search, UserInfo.getInstance().getProjectCode()).subscribe(new io.reactivex.Observer<DataResult<MessageBean>>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(DataResult<MessageBean> dataResult) {
                if (null != dataResult.getResult())
                    if (dataResult.getResult().getCode() == 200) {
                        mAdapter.submitList(dataResult.getResult().getData());

                        if (dataResult.getResult().getData().size()>0) {
                            mBinding.notdata.setVisibility(View.GONE);
                        } else {
                            mBinding.notdata.setVisibility(View.VISIBLE);
                        }
                    }
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    public void getDatas() {
        getDatas("");
    }

    public static class MessageFragmentStates extends StateHolder {

        public final State<String> name = new State<>("");

        public final State<List<WorkOrderDetailBean.DataBean.PtWorkOrderAbnormal>> list = new State<>(new ArrayList<>());

        public final State<Boolean> loadingVisible = new State<>(false);

    }
}
